﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MyOA.BLL;
using MyOA.Model;

namespace MyOA.UI.ScheduleManage.DepartSchedule
{
    public partial class DepartSchedule : System.Web.UI.Page
    {
        protected IList<Schedule> Schedules
        {
            get { return (IList<Schedule>)ViewState["schedules"]; }
            set { ViewState["schedules"] = value; }
        }
        DepartScheduleManage manage = new DepartScheduleManage();
        protected void dataBind(PagedDataSource  pdsShedules)
        {
            if (pdsShedules.Count <= 0)
            {
                divSchedule.Visible = false;
                rpSchedule.DataSource = null;
                rpSchedule.DataBind();

            }
            else
            {
                divSchedule.Visible = true;
                rpSchedule.DataSource = pdsShedules;
               
                rpSchedule.DataBind();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                if (Session["loginId"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;

                }
                if (Request.QueryString["NodeValuePath"] == null)
                {
                    Response.Redirect("~/Error.html");
                    return;
                }
                int nodeId;
                if (int.TryParse(Request.QueryString["NodeValuePath"], out nodeId) == false)
                {
                    Response.Redirect("~/Error.html");
                    return;
                }
                string loginId = (string)Session["loginId"];
                if (new CommandManage().isHaveRoleRight(loginId, nodeId) == false)
                {
                    Response.Redirect("~/Error.html");
                    return;
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int departId = int.Parse(ddlDepart.SelectedValue);
            int userId = 0;
            DateTime dt = new DateTime(1900, 1, 1, 0, 0, 0);
            if (txtLoginId.Text != string.Empty)
            {
                userId = manage.getUserIdByLoginId(txtLoginId.Text);
            }
            if (txtDate.Text != string.Empty)
            {
                dt = DateTime.Parse(txtDate.Text);
            }

            IList<Schedule> schedules = manage.getScheduleByMoreTerm(departId, userId, dt);
            Schedules = schedules;//以便分页中使用
            PagedDataSource pdsShedules = new PagedDataSource();
            pdsShedules.DataSource = schedules; 
            pdsShedules.AllowPaging = true; 
           pdsShedules.CurrentPageIndex =0;
            
            pdsShedules.PageSize = aspDepartSchedule.PageSize;
            aspDepartSchedule.RecordCount = schedules.Count;
            aspDepartSchedule.CurrentPageIndex = 1;

           dataBind(pdsShedules);
          
          
            
        }

        protected void ddlBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ddlDepart.Enabled = true;
            //ddlDepart.DataValueField = "DepartId";
            //ddlDepart.DataTextField = "DepartName";
            //ddlDepart.DataSource = manage.getDepartInfoByBranchId(int.Parse(ddlBranch.SelectedValue));
            //ddlDepart.DataBind();
            
        }

        protected void aspDepartSchedule_PageChanged(object sender, EventArgs e)
        {
            PagedDataSource pdsShedules = new PagedDataSource();
            pdsShedules.DataSource = Schedules;
            pdsShedules.PageSize = aspDepartSchedule.PageSize;
            pdsShedules.AllowPaging = true;
            pdsShedules.CurrentPageIndex = aspDepartSchedule.CurrentPageIndex - 1;
            aspDepartSchedule.RecordCount = Schedules.Count;

            dataBind(pdsShedules);
        }

    }
}
